package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.vue.views;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Index_vue implements CodeGenTemplate {


    @Autowired
    CommonColumn commonColumn;

    private boolean hasImageUploadColumn = false;
    private boolean hasFileUploadColumn = false;
    private boolean hasEditorColumn = false;


    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.vueFilePath("views/" + table.getModuleName() + "/" + localBinding.getClassNameVar() + "/index.vue");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("<template>");
        code.apAndBl("  <div class=\"app-container\">");
        code.bl();
        code.apAndBl("    <!-- 搜索工作栏 -->");
        code.apAndBl("    <el-form :model=\"queryParams\" ref=\"queryForm\" :inline=\"true\" v-show=\"showSearch\" label-width=\"68px\">");
        code.apAndBl("        <el-row>");
        code.apAndBl("            <el-form-item>");
        code.apAndBl("                <el-button type=\"primary\" icon=\"el-icon-search\" size=\"mini\" @click=\"handleQuery\">搜索</el-button>");
        code.apAndBl("                <el-button icon=\"el-icon-refresh\" size=\"mini\" @click=\"resetQuery\">重置</el-button>");
        code.apAndBl("            </el-form-item>");
        code.apAndBl("        </el-row>");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getPrimaryKey())    continue;
            if (!column.getListOperation())    continue;
            String htmlType = column.getHtmlType();
            switch (htmlType) {
                case "input":
                    code.apAndBl("      <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("        <el-input v-model=\"queryParams." + column.getJavaField() + "\" placeholder=\"请输入" + column.getColumnComment() + "\" clearable size=\"small\" @keyup.enter.native=\"handleQuery\"/>");
                    code.apAndBl("      </el-form-item>");
                    break;
                case "select":
                case "radio":
                    code.apAndBl("      <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("        <el-select v-model=\"queryParams." + column.getJavaField() + "\" placeholder=\"请选择" + column.getColumnComment() + "\" clearable size=\"small\">");
                    if (column.getDictType() != null && !"".equals(column.getDictType())) {
                        // 设置了 dictType 数据字典的情况
                        code.apAndBl("          <el-option v-for=\"dict in this.getDictDatas(DICT_TYPE." + column.getDictType().toUpperCase() + ")\"");
                        code.apAndBl("                       :key=\"dict.value\" :label=\"dict.label\" :value=\"dict.value\"/>");
                    } else {
                        // 未设置 dictType 数据字典的情况
                        code.apAndBl("          <el-option label=\"请选择字典生成\" value=\"\" />");
                    }
                    code.apAndBl("        </el-select>");
                    code.apAndBl("      </el-form-item>");
                    break;
                case "datetime":
                    if (!"BETWEEN".equals(column.getListOperationCondition())) {
                        // 非范围
                        code.apAndBl("      <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                        code.apAndBl("        <el-date-picker clearable size=\"small\" v-model=\"queryParams." + column.getJavaField() + "\" type=\"date\" value-format=\"yyyy-MM-dd\" placeholder=\"选择" + column.getColumnComment() + "\" />");
                        code.apAndBl("      </el-form-item>");
                    } else {
                        // 范围
                        code.apAndBl("      <el-form-item label=\"" + column.getColumnComment() + "\">");
                        code.apAndBl("        <el-date-picker v-model=\"dateRange" + StrUtil.upperFirst(column.getJavaField()) + "\" size=\"small\" style=\"width: 240px\" value-format=\"yyyy-MM-dd\"");
                        code.apAndBl("                        type=\"daterange\" range-separator=\"-\" start-placeholder=\"开始日期\" end-placeholder=\"结束日期\" />");
                        code.apAndBl("      </el-form-item>");
                    }
                    break;
                default:
                    break;
            }
        }
        code.apAndBl("    </el-form>");
        code.bl();
        code.apAndBl("    <!-- 操作工具栏 -->");
        code.apAndBl("    <el-row :gutter=\"10\" class=\"mb8\">");
        code.apAndBl("      <el-col :span=\"1.5\">");
        code.apAndBl("        <el-button type=\"primary\" plain icon=\"el-icon-plus\" size=\"mini\" @click=\"handleAdd\"");
        code.apAndBl("                   v-hasPermi=\"['" + localBinding.getPermissionPrefix() + ":create']\">新增</el-button>");
        code.apAndBl("      </el-col>");
        code.apAndBl("      <el-col :span=\"1.5\">");
        code.apAndBl("        <el-button type=\"warning\" plain icon=\"el-icon-download\" size=\"mini\" @click=\"handleExport\" :loading=\"exportLoading\"");
        code.apAndBl("                   v-hasPermi=\"['" + localBinding.getPermissionPrefix() + ":export']\">导出</el-button>");
        code.apAndBl("      </el-col>");
        code.apAndBl("      <right-toolbar :showSearch.sync=\"showSearch\" @queryTable=\"getList\"></right-toolbar>");
        code.apAndBl("    </el-row>");
        code.bl();
        code.apAndBl("    <!-- 列表 -->");
        code.apAndBl("    <el-table v-loading=\"loading\" :data=\"list\">");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (!column.getListOperation()) continue;
            if ("Date".equals(column.getJavaType())) {
                code.apAndBl("      <el-table-column label=\"" + column.getColumnComment() + "\" align=\"center\" prop=\"" + column.getJavaField() + "\" width=\"180\">");
                code.apAndBl("        <template slot-scope=\"scope\">");
                code.apAndBl("          <span>{{ parseTime(scope.row." + column.getJavaField() + ") }}</span>");
                code.apAndBl("        </template>");
                code.apAndBl("      </el-table-column>");
            } else if (column.getDictType() != null && !"".equals(column.getDictType())) {
                // 数据字典
                code.apAndBl("      <el-table-column label=\"" + column.getColumnComment() + "\" align=\"center\" prop=\"" + column.getJavaField() + "\">");
                code.apAndBl("        <template slot-scope=\"scope\">");
                code.apAndBl("          <dict-tag :type=\"DICT_TYPE." + column.getDictType().toUpperCase() + "\" :value=\"scope.row." + column.getJavaField() + "\" />");
                code.apAndBl("        </template>");
                code.apAndBl("      </el-table-column>");
            } else {
                code.apAndBl("      <el-table-column label=\"" + column.getColumnComment() + "\" align=\"center\" prop=\"" + column.getJavaField() + "\" />");
            }
        }
        code.apAndBl("      <el-table-column label=\"操作\" align=\"center\" class-name=\"small-padding fixed-width\">");
        code.apAndBl("        <template slot-scope=\"scope\">");
        code.apAndBl("          <el-button size=\"mini\" type=\"text\" icon=\"el-icon-edit\" @click=\"handleUpdate(scope.row)\"");
        code.apAndBl("                     v-hasPermi=\"['" + localBinding.getPermissionPrefix() + ":update']\">修改</el-button>");
        code.apAndBl("          <el-button size=\"mini\" type=\"text\" icon=\"el-icon-delete\" @click=\"handleDelete(scope.row)\"");
        code.apAndBl("                     v-hasPermi=\"['" + localBinding.getPermissionPrefix() + ":delete']\">删除</el-button>");
        code.apAndBl("        </template>");
        code.apAndBl("      </el-table-column>");
        code.apAndBl("    </el-table>");
        code.apAndBl("    <!-- 分页组件 -->");
        code.apAndBl("    <pagination v-show=\"total > 0\" :total=\"total\" :page.sync=\"queryParams.pageNo\" :limit.sync=\"queryParams.pageSize\"");
        code.apAndBl("                @pagination=\"getList\"/>");
        code.bl();
        code.apAndBl("    <!-- 对话框(添加 / 修改) -->");
        code.apAndBl("    <el-dialog :title=\"title\" :visible.sync=\"open\" width=\"900px\" append-to-body>");
        code.apAndBl("      <el-form ref=\"form\" :model=\"form\" :rules=\"rules\" label-width=\"100px\">");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            String htmlType = column.getHtmlType();
            switch (htmlType) {
                case "input":
                    // 忽略主键，不用在表单里
                    if (!column.getPrimaryKey()) {
                        code.apAndBl("        <el-row>");
                        code.apAndBl("          <el-col :span=\"12\">");
                        code.apAndBl("              <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                        code.apAndBl("                  <el-input v-model=\"form." + column.getJavaField() + "\" placeholder=\"请输入" + column.getColumnComment() + "\" />");
                        code.apAndBl("              </el-form-item>");
                        code.apAndBl("          </el-col>");
                        code.apAndBl("        </el-row>");
                    }
                    break;
                case "imageUpload":
                    hasImageUploadColumn = true;
                    // 图片上传
                    code.apAndBl("        <el-row>");
                    code.apAndBl("          <el-form-item label=\"" + column.getColumnComment() + "\">");
                    code.apAndBl("            <imageUpload v-model=\"form." + column.getJavaField() + "\"/>");
                    code.apAndBl("          </el-form-item>");
                    code.apAndBl("        </el-row>");
                    break;
                case "fileUpload":
                    hasFileUploadColumn = true;
                    // 文件上传
                    code.apAndBl("        <el-row>");
                    code.apAndBl("          <el-form-item label=\"" + column.getColumnComment() + "\">");
                    code.apAndBl("            <fileUpload v-model=\"form." + column.getJavaField() + "\"/>");
                    code.apAndBl("          </el-form-item>");
                    code.apAndBl("        </el-row>");
                    break;
                case "editor":
                    hasEditorColumn = true;
                    // 文本编辑器
                    code.apAndBl("        <el-row>");
                    code.apAndBl("          <el-col :span=\"12\">");
                    code.apAndBl("              <el-form-item label=\"" + column.getColumnComment() + "\">");
                    code.apAndBl("                  <editor v-model=\"form." + column.getJavaField() + "\" :min-height=\"192\"/>");
                    code.apAndBl("              </el-form-item>");
                    code.apAndBl("          </el-col>");
                    code.apAndBl("        </el-row>");
                    break;
                case "select":
                    // 下拉框
                    code.apAndBl("        <el-col :span=\"12\">");
                    code.apAndBl("            <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("                <el-select v-model=\"form." + column.getJavaField() + "\" placeholder=\"请选择" + column.getColumnComment() + "\">");
                    if (column.getDictType() != null && !"".equals(column.getDictType())) {
                        // 有数据字典
                        code.apAndBl("                        <el-option v-for=\"dict in this.getDictDatas(DICT_TYPE." + column.getDictType().toUpperCase() + ")\"");

                        code.ap("                                   :key=\"dict.value\" :label=\"dict.label\" ");
                        if ("Integer".equals(column.getJavaType()) || "Long".equals(column.getJavaType()))
                            code.ap(":value=\"parseInt(dict.value)");
                        else
                            code.ap(":value=\"dict.value\"");
                        code.apAndBl(" />");
                    } else {
                        // 没数据字典
                        code.apAndBl("                        <el-option label=\"请选择字典生成\" value=\"\" />");
                    }
                    code.apAndBl("                </el-select>");
                    code.apAndBl("            </el-form-item>");
                    code.apAndBl("        </el-col>");
                    break;
                case "checkbox":
                    // 多选框
                    code.apAndBl("        <el-col :span=\"12\">");
                    code.apAndBl("            <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("                <el-checkbox-group v-model=\"form." + column.getJavaField() + "\">");
                    if (column.getDictType() != null && !"".equals(column.getDictType())) {
                        // 有数据字典
                        code.apAndBl("                        <el-checkbox v-for=\"dict in this.getDictDatas(DICT_TYPE." + column.getDictType().toUpperCase() + ")\"");

                        code.ap("                                     :key=\"dict.value\" ");
                        if ("Integer".equals(column.getJavaType()) || "Long".equals(column.getJavaType())) {
                            code.ap(":label=\"parseInt(dict.value)\"");
                        } else {
                            code.ap(":label=\"dict.value\"");
                        }
                        code.apAndBl(">{{dict.label}}</el-checkbox>");
                    } else {
                        // 没数据字典
                        code.apAndBl("                        <el-checkbox>请选择字典生成</el-checkbox>");
                    }
                    code.apAndBl("                </el-checkbox-group>");
                    code.apAndBl("            </el-form-item>");
                    code.apAndBl("        </el-col>");
                    break;
                case "radio":
                    // 单选框
                    code.apAndBl("        <el-col :span=\"12\">");
                    code.apAndBl("            <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("                <el-radio-group v-model=\"form." + column.getJavaField() + "\">");
                    if (column.getDictType() != null && !"".equals(column.getDictType())) {
                        // 有数据字典
                        code.apAndBl("                        <el-radio v-for=\"dict in this.getDictDatas(DICT_TYPE." + column.getDictType().toUpperCase() + ")\"");

                        code.ap("                                  :key=\"dict.value\" ");
                        if ("Integer".equals(column.getJavaType()) || "Long".equals(column.getJavaType())) {
                            code.ap(":label=\"parseInt(dict.value)\"");
                        } else {
                            code.ap(":label=\"dict.value\"");
                        }
                        code.apAndBl(">{{dict.label}}</el-radio>");
                    } else {
                        // 没数据字典
                        code.apAndBl("                        <el-radio label=\"1\">请选择字典生成</el-radio>");
                    }
                    code.apAndBl("                </el-radio-group>");
                    code.apAndBl("            </el-form-item>");
                    code.apAndBl("        </el-col>");
                    break;
                case "datetime":
                    // 时间框
                    code.apAndBl("        <el-col :span=\"12\">");
                    code.apAndBl("            <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("                <el-date-picker clearable size=\"small\" v-model=\"form." + column.getJavaField() + "\" type=\"date\" value-format=\"yyyy-MM-dd\" placeholder=\"选择" + column.getColumnComment() + "\" />");
                    code.apAndBl("            </el-form-item>");
                    code.apAndBl("        </el-col>");
                    break;
                case "textarea":
                    // 文本框
                    code.apAndBl("        <el-col :span=\"12\">");
                    code.apAndBl("            <el-form-item label=\"" + column.getColumnComment() + "\" prop=\"" + column.getJavaField() + "\">");
                    code.apAndBl("                <el-input v-model=\"form." + column.getJavaField() + "\" type=\"textarea\" placeholder=\"请输入内容\" />");
                    code.apAndBl("            </el-form-item>");
                    code.apAndBl("        </el-col>");
                    break;
                default:
                    break;
            }
        }

        code.apAndBl("      </el-form>");
        code.apAndBl("      <div slot=\"footer\" class=\"dialog-footer\">");
        code.apAndBl("        <el-button type=\"primary\" @click=\"submitForm\">确 定</el-button>");
        code.apAndBl("        <el-button @click=\"cancel\">取 消</el-button>");
        code.apAndBl("      </div>");
        code.apAndBl("    </el-dialog>");
        code.apAndBl("  </div>");
        code.apAndBl("</template>");
        code.bl();
        code.apAndBl("<script>");
        code.apAndBl("import { create" + localBinding.getSimpleClassName() + ", update" + localBinding.getSimpleClassName() + ", delete" + localBinding.getSimpleClassName() + ", get" + localBinding.getSimpleClassName() + ", get" + localBinding.getSimpleClassName() + "Page, export" + localBinding.getSimpleClassName() + "Excel } from \"@/api/" + table.getModuleName() + "/" + localBinding.getClassNameVar() + "\";");
        if (hasImageUploadColumn) {
            code.apAndBl("import ImageUpload from '@/components/ImageUpload';");
        }
        if (hasFileUploadColumn) {
            code.apAndBl("import FileUpload from '@/components/FileUpload';");
        }
        if (hasEditorColumn) {
            code.apAndBl("import Editor from '@/components/Editor';");
        }
        code.bl();
        code.apAndBl("export default {");
        code.apAndBl("  name: \"" + localBinding.getSimpleClassName() + "\",");
        code.apAndBl("  components: {");
        if (hasImageUploadColumn) {
            code.apAndBl("    ImageUpload,");
        }
        if (hasFileUploadColumn) {
            code.apAndBl("    FileUpload,");
        }
        if (hasEditorColumn) {
            code.apAndBl("    Editor,");
        }
        code.apAndBl("  },");
        code.apAndBl("  data() {");
        code.apAndBl("    return {");
        code.apAndBl("      // 遮罩层");
        code.apAndBl("      loading: true,");
        code.apAndBl("      // 导出遮罩层");
        code.apAndBl("      exportLoading: false,");
        code.apAndBl("      // 显示搜索条件");
        code.apAndBl("      showSearch: true,");
        code.apAndBl("      // 总条数");
        code.apAndBl("      total: 0,");
        code.apAndBl("      // " + table.getClassComment() + "列表");
        code.apAndBl("      list: [],");
        code.apAndBl("      // 弹出层标题");
        code.apAndBl("      title: \"\",");
        code.apAndBl("      // 是否显示弹出层");
        code.apAndBl("      open: false,");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getListOperation() && "datetime".equals(column.getHtmlType()) && "BETWEEN".equals(column.getListOperationCondition())) {
                code.apAndBl("      dateRange" + StrUtil.upperFirst(column.getJavaField()) + ": [],");
            }
        }
        code.apAndBl("      // 查询参数");
        code.apAndBl("      queryParams: {");
        code.apAndBl("        pageNo: 1,");
        code.apAndBl("        pageSize: 10,");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getListOperation() && !"BETWEEN".equals(column.getListOperationCondition())) {
                code.apAndBl("        " + column.getJavaField() + ": null,");
            }
        }
        code.apAndBl("      },");
        code.apAndBl("      // 表单参数");
        code.apAndBl("      form: {},");
        code.apAndBl("      // 表单校验");
        code.apAndBl("      rules: {");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // 创建或者更新操作 && 要求非空 && 非主键
            if ((column.getCreateOperation() || column.getUpdateOperation()) && !column.getNullable() && !column.getPrimaryKey()) {
                code.ap("        " + column.getJavaField() + ": [{ required: true, message: \"" + column.getColumnComment() + "不能为空\", trigger: ");
                if ("select".equals(column.getHtmlType())) {
                    code.ap("\"change\"");
                } else {
                    code.ap("\"blur\"");
                }
                code.apAndBl(" }],");
            }
        }
        code.apAndBl("      }");
        code.apAndBl("    };");
        code.apAndBl("  },");
        code.apAndBl("  created() {");
        code.apAndBl("    this.getList();");
        code.apAndBl("  },");
        code.apAndBl("  methods: {");
        code.apAndBl("    /** 查询列表 */");
        code.apAndBl("    getList() {");
        code.apAndBl("      this.loading = true;");
        code.apAndBl("      // 处理查询参数");
        code.apAndBl("      let params = {...this.queryParams};");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getListOperation() && "datetime".equals(column.getHtmlType()) && "BETWEEN".equals(column.getListOperationCondition())) {
                code.apAndBl("      this.addBeginAndEndTime(params, this.dateRange" + StrUtil.upperFirst(column.getJavaField()) + ", '" + column.getJavaField() + "');");
            }
        }
        code.apAndBl("      // 执行查询");
        code.apAndBl("      get" + localBinding.getSimpleClassName() + "Page(params).then(response => {");
        code.apAndBl("        this.list = response.data.list;");
        code.apAndBl("        this.total = response.data.total;");
        code.apAndBl("        this.loading = false;");
        code.apAndBl("      });");
        code.apAndBl("    },");
        code.apAndBl("    /** 取消按钮 */");
        code.apAndBl("    cancel() {");
        code.apAndBl("      this.open = false;");
        code.apAndBl("      this.reset();");
        code.apAndBl("    },");
        code.apAndBl("    /** 表单重置 */");
        code.apAndBl("    reset() {");
        code.apAndBl("      this.form = {");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if ((column.getCreateOperation() || column.getUpdateOperation())
                    && "checkbox".equals(column.getHtmlType())) {
                code.apAndBl("        " + column.getJavaField() + ": [],");
            } else {
                code.apAndBl("        " + column.getJavaField() + ": undefined,");
            }
        }
        code.apAndBl("      };");
        code.apAndBl("      this.resetForm(\"form\");");
        code.apAndBl("    },");
        code.apAndBl("    /** 搜索按钮操作 */");
        code.apAndBl("    handleQuery() {");
        code.apAndBl("      this.queryParams.pageNo = 1;");
        code.apAndBl("      this.getList();");
        code.apAndBl("    },");
        code.apAndBl("    /** 重置按钮操作 */");
        code.apAndBl("    resetQuery() {");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getListOperation()
                    && "datetime".equals(column.getHtmlType()) && "BETWEEN".equals(column.getListOperationCondition())) {
                code.apAndBl("      this.dateRange" + StrUtil.upperFirst(column.getJavaField()) + " = [];");
            }
        }
        code.apAndBl("      this.resetForm(\"queryForm\");");
        code.apAndBl("      this.handleQuery();");
        code.apAndBl("    },");
        code.apAndBl("    /** 新增按钮操作 */");
        code.apAndBl("    handleAdd() {");
        code.apAndBl("      this.reset();");
        code.apAndBl("      this.open = true;");
        code.apAndBl("      this.title = \"添加" + table.getClassComment() + "\";");
        code.apAndBl("    },");
        code.apAndBl("    /** 修改按钮操作 */");
        code.apAndBl("    handleUpdate(row) {");
        code.apAndBl("      this.reset();");
        code.apAndBl("      const " + primaryColumn.getJavaField() + " = row." + primaryColumn.getJavaField() + ";");
        code.apAndBl("      get" + localBinding.getSimpleClassName() + "(" + primaryColumn.getJavaField() + ").then(response => {");
        code.apAndBl("        this.form = response.data;");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // checkbox 特殊处理
            if ("checkbox".equals(column.getHtmlType())) {
                code.apAndBl("        this.form." + column.getJavaField() + " = this.form." + column.getJavaField() + ".split(\",\");");
            }
        }
        code.apAndBl("        this.open = true;");
        code.apAndBl("        this.title = \"修改" + table.getClassComment() + "\";");
        code.apAndBl("      });");
        code.apAndBl("    },");
        code.apAndBl("    /** 提交按钮 */");
        code.apAndBl("    submitForm() {");
        code.apAndBl("      this.$refs[\"form\"].validate(valid => {");
        code.apAndBl("        if (!valid) {");
        code.apAndBl("          return;");
        code.apAndBl("        }");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // checkbox 特殊处理
            if ("checkbox".equals(column.getHtmlType())) {
                code.apAndBl("        this.form." + column.getJavaField() + " = this.form." + column.getJavaField() + ".join(\",\");");
            }
        }
        code.apAndBl("        // 修改的提交");
        code.apAndBl("        if (this.form." + primaryColumn.getJavaField() + " != null) {");
        code.apAndBl("          update" + localBinding.getSimpleClassName() + "(this.form).then(response => {");
        code.apAndBl("            this.$modal.msgSuccess(\"修改成功\");");
        code.apAndBl("            this.open = false;");
        code.apAndBl("            this.getList();");
        code.apAndBl("          });");
        code.apAndBl("          return;");
        code.apAndBl("        }");
        code.apAndBl("        // 添加的提交");
        code.apAndBl("        create" + localBinding.getSimpleClassName() + "(this.form).then(response => {");
        code.apAndBl("          this.$modal.msgSuccess(\"新增成功\");");
        code.apAndBl("          this.open = false;");
        code.apAndBl("          this.getList();");
        code.apAndBl("        });");
        code.apAndBl("      });");
        code.apAndBl("    },");
        code.apAndBl("    /** 删除按钮操作 */");
        code.apAndBl("    handleDelete(row) {");
        code.apAndBl("      const " + primaryColumn.getJavaField() + " = row." + primaryColumn.getJavaField() + ";");
        code.apAndBl("      this.$modal.confirm('是否确认删除" + table.getClassComment() + "编号为\"' + " + primaryColumn.getJavaField() + " + '\"的数据项?').then(function() {");
        code.apAndBl("          return delete" + localBinding.getSimpleClassName() + "(" + primaryColumn.getJavaField() + ");");
        code.apAndBl("        }).then(() => {");
        code.apAndBl("          this.getList();");
        code.apAndBl("          this.$modal.msgSuccess(\"删除成功\");");
        code.apAndBl("        }).catch(() => {});");
        code.apAndBl("    },");
        code.apAndBl("    /** 导出按钮操作 */");
        code.apAndBl("    handleExport() {");
        code.apAndBl("      // 处理查询参数");
        code.apAndBl("      let params = {...this.queryParams};");
        code.apAndBl("      params.pageNo = undefined;");
        code.apAndBl("      params.pageSize = undefined;");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getListOperation()
                    && "datetime".equals(column.getHtmlType()) && "BETWEEN".equals(column.getListOperationCondition())) {
                code.apAndBl("      this.addBeginAndEndTime(params, this.dateRange" + StrUtil.upperFirst(column.getJavaField()) + ", '" + primaryColumn.getJavaField() + "');");
            }
        }
        code.apAndBl("      // 执行导出");
        code.apAndBl("      this.$modal.confirm('是否确认导出所有" + table.getClassComment() + "数据项?').then(() => {");
        code.apAndBl("          this.exportLoading = true;");
        code.apAndBl("          return export" + localBinding.getSimpleClassName() + "Excel(params);");
        code.apAndBl("        }).then(response => {");
        code.apAndBl("          this.$download.excel(response, '" + table.getClassComment() + ".xls');");
        code.apAndBl("          this.exportLoading = false;");
        code.apAndBl("        }).catch(() => {});");
        code.apAndBl("    }");
        code.apAndBl("  }");
        code.apAndBl("};");
        code.apAndBl("</script>");
        code.bl();

        return code.toString();
    }
}
